package org.hj.modules.demo.test.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import org.hj.modules.demo.test.entity.HjOrderCustomer;
import org.hj.modules.demo.test.entity.HjOrderMain;
import org.hj.modules.demo.test.entity.HjOrderTicket;
import org.hj.modules.demo.test.mapper.HjOrderCustomerMapper;
import org.hj.modules.demo.test.mapper.HjOrderMainMapper;
import org.hj.modules.demo.test.mapper.HjOrderTicketMapper;
import org.hj.modules.demo.test.service.IHjOrderMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 *订单
 *
 */
@Service
public class HjOrderMainServiceImpl extends ServiceImpl<HjOrderMainMapper, HjOrderMain> implements IHjOrderMainService {

    @Autowired
    private HjOrderMainMapper hjOrderMainMapper;
    @Autowired
    private HjOrderCustomerMapper hjOrderCustomerMapper;
    @Autowired
    private HjOrderTicketMapper hjOrderTicketMapper;

    @Override
    @Transactional
    public void saveMain(HjOrderMain hjOrderMain, List<HjOrderCustomer> hjOrderCustomerList, List<HjOrderTicket> hjOrderTicketList) {
        hjOrderMainMapper.insert(hjOrderMain);
        if (hjOrderCustomerList != null) {
            for (HjOrderCustomer entity : hjOrderCustomerList) {
                entity.setOrderId(hjOrderMain.getId());
                hjOrderCustomerMapper.insert(entity);
            }
        }
        if (hjOrderTicketList != null) {
            for (HjOrderTicket entity : hjOrderTicketList) {
                entity.setOrderId(hjOrderMain.getId());
                hjOrderTicketMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional
    public void updateMain(HjOrderMain hjOrderMain, List<HjOrderCustomer> hjOrderCustomerList, List<HjOrderTicket> hjOrderTicketList) {
        hjOrderMainMapper.updateById(hjOrderMain);

        //1.先删除子表数据
        hjOrderTicketMapper.deleteTicketsByMainId(hjOrderMain.getId());
        hjOrderCustomerMapper.deleteCustomersByMainId(hjOrderMain.getId());

        //2.子表数据重新插入
        if (hjOrderCustomerList != null) {
            for (HjOrderCustomer entity : hjOrderCustomerList) {
                entity.setOrderId(hjOrderMain.getId());
                hjOrderCustomerMapper.insert(entity);
            }
        }
        if (hjOrderTicketList != null) {
            for (HjOrderTicket entity : hjOrderTicketList) {
                entity.setOrderId(hjOrderMain.getId());
                hjOrderTicketMapper.insert(entity);
            }
        }
    }

	@Override
	@Transactional
	public void delMain(String id) {
        hjOrderMainMapper.deleteById(id);
		hjOrderTicketMapper.deleteTicketsByMainId(id);
        hjOrderCustomerMapper.deleteCustomersByMainId(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			hjOrderMainMapper.deleteById(id);
			hjOrderTicketMapper.deleteTicketsByMainId(id.toString());
            hjOrderCustomerMapper.deleteCustomersByMainId(id.toString());
		}
	}

}
